/*-+-*****************************************************************************
 Copyright (C) 2009-2012 Oceanside Software Corporation (R)
 Dallas, Texas

 Author       : Jason T. Brower
 Purpose      : This file is part of a point-of-sale software package
                originally written for Table-Service Restaurants.
              
 Date         : July 8th of 2012
              
 History      : The application that this software file belongs to was 
                written over a 3-Year period.  The first version was written
                in C# .Net 3.5 using Windows Forms.  Being new to .Net at
                the time, I soon learned of Windows Presentation Foundation,
                liked the graphics capabilities and databinding so I
                migrated to WPF.  When the .Net 4.0 framework was released
                the application was migrated from 3.5 to 4.0. Now today,
                .Net 4.5 RC is out and I was already in process of migrating
                before I made this public.
                
Excuse Me     : Please excuse the coding style, it is not representative of
                my typical work.  During the three years I was under a very
                tight budget, constantly running out of funding and was 
                working 18 hour days hyped up on 5-Hour Energy.  The 
                design is not textbook.  It is monolythic because of
                previous issues I will not get into.  There are few comments
                in the code and the ones that are there may be incorrect.
                
What it is not: This is not the exact version that I took through PA-DSS 2.0
                nor is it the exact version running at my customer sites.
                I removed some details for security purposes including some
                encryption algorithms.  The reporting was removed because
                I was including a third party library that cannot be 
                published with this Open Source.
                
What it is    : This application has been running at high volume restaraunts
                for over a year.  It never freezes, crashes or does anything
                unexpected.  So long as it is built, installed and configured
                correctly, it is a strong system that we can all enhance.

Contact       : Please note that I can no longer work for free.  However, you
                can reach me at information@oceansidesw.com.                                                      

*********************************************************************************/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
#if _LOCALDB
  using Oceanside.LocalDB;
#else
using Oceanside.RemoteDB;
#endif
namespace Oceanside.CloseCheck
{
    public static class GlobalStateObject
    {
        public static bool GlobalContextNotNull;
        public static bool SelectedShiftNotNull;
        public static bool SystemHasCompAccounts;
        public static bool SystemHasPromoAccounts;
        public static bool SystemHasHouseAccounts;
        public static bool SystemAllowsCardOnFile;
        public static bool SystemHasLocalPrinter;
        public static bool SystemHasCashDrawer;
        public static bool SystemHasVoidAccounts;
 
        public static void Refresh()
        {
            try
            {
                Falsify();
                GlobalContextNotNull = SHIFT.GlobalContext != null;
                SelectedShiftNotNull = SHIFT.Current != null;
                SystemHasCompAccounts = SHIFT.GlobalContext.COMP_ACCOUNTS.Where(ca => ca.IsComp && !ca.IsDeleted).Any();
                SystemHasPromoAccounts = SHIFT.GlobalContext.COMP_ACCOUNTS.Where(ca => (ca.IsPromo) && !ca.IsDeleted).Any();
                SystemHasHouseAccounts = SHIFT.GlobalContext.HOUSE_ACCOUNTS.Where(ha => ha.IsDeleted == false && ha.Active).Any();
                SystemAllowsCardOnFile = SHIFT.GlobalContext.SYSCONFIGS.Where(sc => sc.AllowCardOnFile).Any();
                SystemHasVoidAccounts = SHIFT.GlobalContext.COMP_ACCOUNTS.Where(ca => !ca.IsDeleted && ca.IsVoid).Any();

                string hostName = KIOSK.Hostname();

                SystemHasLocalPrinter = SHIFT.GlobalContext.
                    KIOSKS.
                    Where(k => !k.IsDeleted && k.Name != null && k.Name.Equals(hostName)
                        && k.LocalPrinterId != null && k.LocalPrinterId != 0).Any();

                SystemHasCashDrawer = SHIFT.GlobalContext.
                    KIOSKS.
                    Where(k => !k.IsDeleted && k.Name != null && k.Name.Equals(hostName)
                        && k.LocalPrinterId != null && k.LocalPrinterId != 0 && k.NumberOfCashDrawers != 0).Any();
            }
            catch (Exception ex)
            {
                Logging.LogException(ex);
            }
        }

        public static void Falsify()
        {
            GlobalContextNotNull = false;
            SelectedShiftNotNull = false;
            SystemHasCompAccounts = false;
            SystemHasPromoAccounts = false;
            SystemHasHouseAccounts = false;
            SystemAllowsCardOnFile = false;
            SystemHasLocalPrinter = false;
            SystemHasCashDrawer = false;
            SystemHasVoidAccounts = false;
        }
    }

    public static class StateObject
    {
        public static bool SelectedTransactionNotNull;
        public static bool SelectedTransactionTypeIsCredit;
        public static bool SelectedTransactionIsAccount;
        public static bool SelectedTransactionWasVoided;
        public static bool SelectedTransactionIsCash;
        public static bool SelectedTransactionIsCheck;
        public static bool SelectedTransactionIsGift;

        public static bool SelectedOrderNotNull;
        public static bool SelectedOrderHasNoTransactions;
        public static bool CanCloseSelectedOrder;
        public static bool SelectedOrderIsClosed;
        public static bool SelectedOrderHasSales;
        public static bool SelectedOrderHasCardOnFile;
        public static bool SelectedOrderCheckCountIsOne;
        public static bool SelectedOrderGuestCountIsOne;
        public static bool SelectedSaleNotNull;
        public static bool SelectedSaleIsGiftCardSale;
        public static bool SelectedSaleDiscountIdIsNullOrZero;
        public static bool SelectedSaleIsForcedModifier;
        public static bool CanCloseSelectedCheck;
        public static bool SelectedChecksSalesIsZero;
        public static bool SelectedCheckHasCompableSales;
        public static bool CurrentUserCanPromoCheck;
        public static bool CurrentUserCanCompItem;
        public static bool CurrentUserCanVoidPayment;
        public static bool CurrentUserCanApplyHousePayment;
        public static bool CurrentUserCanNoSale;
        public static bool CurrentUserCanVoidItem;

        public static int CurrentUsersOrderCount = 0;
        public static int CurrentViewedOrderCounter = 0;

        public static void Refresh()
        {
            try
            {
                Falsify();

                SHIFT shift = SHIFT.Current;
                if (shift != null)
                {
                    TRANSACTION transaction = CCBindings.SelectedTransaction;
                    SelectedTransactionNotNull = transaction != null;
                    if (SelectedTransactionNotNull)
                    {
                        SelectedTransactionTypeIsCredit = (transaction.TransactionType == TRANSACTION.CONST_PAY_CREDIT);
                        SelectedTransactionIsAccount = (transaction.TransactionType == TRANSACTION.CONST_PAY_ACCOUNT);
                        SelectedTransactionWasVoided = (transaction.WasVoided);
                        SelectedTransactionIsCash = (transaction.TransactionType == TRANSACTION.CONST_PAY_CASH);
                        SelectedTransactionIsCheck = (transaction.TransactionType == TRANSACTION.CONST_PAY_CHECK);
                        SelectedTransactionIsGift = (transaction.TransactionType == TRANSACTION.CONST_PAY_GIFT);
                    }

                    ORDER order = SHIFT.SelectedOrder;
                    SelectedOrderNotNull = order != null;
                    if (order != null)
                    {
                        SelectedOrderHasNoTransactions = order.TRANSACTIONS.Count == 0;
                        CanCloseSelectedOrder = order.CanCloseOrder;
                        SelectedOrderIsClosed = order.IsClosed;
                        SelectedOrderHasSales = order.SALES.Count != 0;
                        SelectedOrderHasCardOnFile = order.HasCardOnFile;
                        SelectedOrderCheckCountIsOne = order.CheckCount == 1;
                        SelectedOrderGuestCountIsOne = order.GuestCount == 1;

                        int selectedCheckIndex = CCBindings.SelectedSeatIndex;
                        List<SALE> selChecksSales = order.GetChecksSales(selectedCheckIndex);
                        CanCloseSelectedCheck = order.CanCloseCheck;
                        SelectedChecksSalesIsZero = selChecksSales == null || selChecksSales.Count == 0;
                        SelectedCheckHasCompableSales = order.RoundedChecksCompableSalesSubotal(selectedCheckIndex) != 0;
                    }

                    SALE sale = CloseCheckWindow.CustomerTicketListView.SelectedItem as SALE;
                    SelectedSaleNotNull = sale != null;
                    if (SelectedSaleNotNull)
                    {
                        SelectedSaleIsGiftCardSale = sale.IsGiftCardSale;
                        SelectedSaleDiscountIdIsNullOrZero = sale.DiscountId == null || sale.DiscountId == 0;
                        SelectedSaleIsForcedModifier = sale.IsForcedModifier;
                    }


                    CurrentUserCanPromoCheck = shift.UserCanPromo();
                    CurrentUserCanCompItem = shift.UserCanVoidItem();
                    CurrentUserCanVoidPayment = shift.UserCanVoidPayment();
                    CurrentUserCanApplyHousePayment = shift.UserCanApplyHousePayment();
                    CurrentUserCanNoSale = shift.UserCanNoSale();
                    CurrentUserCanVoidItem = shift.UserCanVoidItem();
                    CurrentUsersOrderCount = shift.ORDERS.Count;
                }
            }

            catch (Exception e)
            {
                Logging.LogException(e);
            }
        }

        public static void Falsify()
        {
            SelectedTransactionNotNull = false;
            SelectedTransactionTypeIsCredit = false;
            SelectedTransactionIsAccount = false;
            SelectedTransactionWasVoided = false;
            SelectedTransactionIsCash = false;
            SelectedTransactionIsCheck = false;
            SelectedTransactionIsGift = false;
            SelectedOrderNotNull = false;
            SelectedOrderHasNoTransactions = false;
            CanCloseSelectedOrder = false;
            SelectedOrderIsClosed = false;
            SelectedOrderHasCardOnFile = false;
            SelectedOrderCheckCountIsOne = false;
            SelectedOrderGuestCountIsOne = false;
            SelectedSaleNotNull = false;
            SelectedSaleIsGiftCardSale = false;
            SelectedSaleDiscountIdIsNullOrZero = false;
            SelectedSaleIsForcedModifier = false;
            CanCloseSelectedCheck = false;
            SelectedChecksSalesIsZero = false;
            SelectedCheckHasCompableSales = false;
            CurrentUserCanPromoCheck = false;
            CurrentUserCanCompItem = false;
            CurrentUserCanVoidPayment = false;
            CurrentUserCanApplyHousePayment = false;
            CurrentUserCanNoSale = false;
            CurrentUserCanVoidItem = false;
        }
    }
}
